import 'dart:convert' show json;

import 'package:flutter/cupertino.dart';

import 'base_chart_formatter.dart';
import 'base_model_asT.dart';
import 'package:ribo_base_core/base_styles/base_chart_style.dart';

class BaseChartCircularDiagramModel {

  /// https://echarts.apache.org/zh/option.html#tooltip.formatter
  BaseChartFormatter tooltipFormatter;

  /// https://echarts.apache.org/zh/option.html#series-pie.label.formatter
  BaseChartFormatter labelFormatter;

  /// 图表样式
  BaseChartStyle style;

  /// 图表数据
  List<BaseChartCircularDiagramData> data;

  BaseChartCircularDiagramModel({
    this.tooltipFormatter,
    this.labelFormatter,
    this.style,
    @required this.data,
  });

  factory BaseChartCircularDiagramModel.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<BaseChartCircularDiagramData> data =
        jsonRes['data'] is List ? <BaseChartCircularDiagramData>[] : null;
    if (data != null) {
      for (final dynamic item in jsonRes['data']) {
        if (item != null) {
          data.add(BaseChartCircularDiagramData.fromJson(
              asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return BaseChartCircularDiagramModel(
      tooltipFormatter: asT<BaseChartFormatter>(jsonRes['tooltipFormatter']),
      labelFormatter: asT<BaseChartFormatter>(jsonRes['labelFormatter']),
      style: asT<BaseChartStyle>(jsonRes['style']),
      data: data,
    );
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'tooltipFormatter': tooltipFormatter,
        'labelFormatter': labelFormatter,
        'style': style,
        'data': data,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartCircularDiagramData {
  BaseChartCircularDiagramData({
    String name,
    this.value,
  }):this.name=name.replaceFirst('-', '-\n').replaceAll(' ', '\n');

  factory BaseChartCircularDiagramData.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : BaseChartCircularDiagramData(
              name: asT<String>(jsonRes['name']),
              value: asT<double>(jsonRes['value']),
            );

  /// 数据名称
  String name;

  /// 数据值
  double value;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'name': name,
        'value': value,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
